package com.example.demo2.controller;

import com.example.demo2.bean.UserBean;
import com.example.demo2.service.UserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class UserBeanController {
    @Autowired
    UserBeanService yourBeanService;

    // 登录
    @RequestMapping("/user/login")
    public Map<String, Object> getMember(@RequestBody UserBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean yourBean1 = yourBeanService.login(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("data",yourBean1);
        } else {
            map.put("success", false);
            map.put("code", 1);
            map.put("msg", "失败");
        }
        return map;
    }

    // 注册
    @RequestMapping("/user/register")
    public Map<String, Object> addMember(@RequestBody UserBean yourBean) {
        Map<String, Object> map = new HashMap<>();

        UserBean panding = new UserBean(yourBean.getUser_accountnumber());
        UserBean yb = yourBeanService.findone(panding);
        if (yb != null){
            map.put("success", false);
            map.put("msg", "账号已经注册");
            map.put("code", 0);
        }else {
            UserBean yourBean1 = yourBeanService.register(yourBean);
            if (yourBean1 != null) {
                map.put("success", true);
                map.put("msg", "成功");
                map.put("code", 0);
            } else {
                map.put("success", false);
                map.put("msg", "失败");
                map.put("code", 1);
            }
        }

        return map;
    }

    // 查询一个
    @RequestMapping("/user/findone")
    public Map<String, Object> findone(@RequestBody UserBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean yourBean1 = yourBeanService.findone(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("data", yourBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }

    // 查询所有用户
    @RequestMapping("/user/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<UserBean> list = yourBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }

    //文件上传
    @RequestMapping("/user/fileUpload")
    public Map<String, Object> fileUpload2(@RequestParam(value = "file") MultipartFile file){
        Map<String, Object> map = new HashMap<>();
        if (file.isEmpty()){
            map.put("success", false);
            map.put("msg", "文件为空");
            map.put("code", 1);
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();//文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
        String  filePath = "/tmp/xmdata/";//上传后的路径
        fileName = UUID.randomUUID() + suffixName;//新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = fileName;
        map.put("success", true);
        map.put("msg", "成功");
        map.put("code", 0);
        map.put("url","/"+ filename);
        return map;
    }
    //文件上传
    @RequestMapping("/user/fileUploadInto")
    public Map<String, Object> fileUpload3(@RequestParam(value = "file") MultipartFile file,String url){
        Map<String, Object> map = new HashMap<>();
        if (file.isEmpty()){
            map.put("success", false);
            map.put("msg", "文件为空");
            map.put("code", 1);
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();//文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
        String  filePath = "/tmp/xmdata/"+url;//上传后的路径
        fileName = UUID.randomUUID() + suffixName;//新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = fileName;
        map.put("success", true);
        map.put("msg", "成功");
        map.put("code", 0);
        map.put("url","/xmdata/"+url+ filename);
        return map;
    }
    //licx文件上传
    @PostMapping("/postfile")
    public Object fileUpload(@RequestParam(value = "userImg", required = false) MultipartFile file, @RequestParam(value = "userId", required = false) Long userId) {
        return yourBeanService.fileUpload(file);
    }

    // 获取轮播图
    @RequestMapping("/user/swiper")
    public Map<String, Object> sw() {
        Map<String, Object> map = new HashMap<>();
//        List<UserBean> list = yourBeanService.list();
//        if (list != null) {
            String[] imgs = {
                    "/images/1.jpg",
                    "/images/2.jpg",
                    "/images/3.jpg",
                    "/images/4.jpg",
                    "/images/5.jpg",
                    "/images/6.jpg",
                    "/images/7.jpg",
                    "/images/8.jpg"
            };
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("list", imgs);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//            map.put("code", 1);
//        }
        return map;
    }

//    // 删除
//    @RequestMapping("/YourServer/yourbean/delete")
//    public Map<String, Object> delete(@RequestBody UserBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        try {
//            yourBeanService.deleteById(yourBean.getId());
//            map.put("success", true);
//            map.put("msg", "成功");
//        } catch (Exception e) {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//
//        return map;
//    }
    // 修改
    @RequestMapping("/user/update")
    public Map<String, Object> update(@RequestBody UserBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean yourBean1 = yourBeanService.update(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("data", yourBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
//    // 添加
//    @RequestMapping("/YourServer/yourbean/addone")
//    public Map<String, Object> addone(@RequestBody UserBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        UserBean yourBean1 = yourBeanService.addone(yourBean);
//        if (yourBean1 != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//            map.put("data", yourBean1);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }
}

